package com.example.demoandroidbki;

import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends Activity {

	private Button m_btn1, m_btn2, m_btn3, m_start, m_stop, m_reset;
	boolean stop = false;
	int progressStatus = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		m_btn1 = (Button) findViewById(R.id.btn1);
		m_btn2 = (Button) findViewById(R.id.btn2);
		m_btn3 = (Button) findViewById(R.id.btn3);
		
		m_btn1.setOnLongClickListener(new View.OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				startShake(findViewById(R.id.btn1));
				//AlertDialog myAlertDialog = taoMotAlertDialog();
				//myAlertDialog.show();
				return false;
			}
		});
		
		m_btn1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				m_btn1.clearAnimation();
			}
		});
		
		m_btn2.setOnLongClickListener(new View.OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				startShake(findViewById(R.id.btn2));
				AlertDialog myAlertDialog = taoMotAlertDialog();
				myAlertDialog.show();
				return false;
			}
		});
		
		m_btn2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				m_btn2.clearAnimation();
			}
		});
		
		m_btn3.setOnLongClickListener(new View.OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				Vibrator vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE) ;
				vibe.vibrate(100);
				return false;
			}
		});
		/////////////Progress bar
		final ProgressBar progressHorizontal = (ProgressBar) findViewById(R.id.progress_horizontal);
        setProgress(progressHorizontal.getProgress() * 100);
        
        final TextProgressBar textProgressBar = (TextProgressBar) findViewById(R.id.progressBarWithText);
        setProgress(progressHorizontal.getProgress() * 100);
		
        m_start = (Button) findViewById(R.id.btnStart);
        m_start.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				progressStatus++;
				progressHorizontal.incrementProgressBy(1);
				// Title progress is in range 0..10000
				setProgress(100 * progressHorizontal.getProgress());
				
				textProgressBar.incrementProgressBy(1);
				setProgress(100 * progressHorizontal.getProgress());
				if(progressStatus <= 100)
					textProgressBar.setText("Loading " + progressStatus + "%");
				
			}
		});
        
        m_stop = (Button) findViewById(R.id.btnStop);
        m_stop.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				progressStatus--;
				progressHorizontal.incrementProgressBy(-1);
				// Title progress is in range 0..10000
				setProgress(100 * progressHorizontal.getProgress());
				
				textProgressBar.incrementProgressBy(-1);
				setProgress(100 * progressHorizontal.getProgress());
				if(progressStatus >= 0)
					textProgressBar.setText("Loading " + progressStatus + "%");
			}
		});
		
		//requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		//setProgressBarVisibility(true);
        
        
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void startShake(View v) {
	    Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
	    v.startAnimation(shake);
	}
	
	private AlertDialog taoMotAlertDialog(){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("AlertDialog");
		builder.setMessage("Nay thi Dialog .................");
		builder.setCancelable(false);
		builder.setNeutralButton("Cancel", null);
		builder.setNegativeButton("Exit",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,
							int which) {
						finish();
					}
				});
		AlertDialog dialog = builder.create();		
		return dialog;
	}
}
